package com.example.controller;

import com.example.dto.CategoryDto;
import com.example.pojo.Category;
import com.example.pojo.Result;
import com.example.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 宿舍分类列表
     * @return
     */
    @GetMapping
    public Result<List<Category>> list(){
        List<Category> list = categoryService.list();
        return Result.success(list);
    }

    /**
     * 新增宿舍分类
     * @param categoryDto
     * @return
     */
    @PostMapping
    public Result AddCategory(@RequestBody CategoryDto categoryDto){
        Category category = new Category();
        BeanUtils.copyProperties(categoryDto,category);
        categoryService.AddCategory(category);
        return Result.success("新增宿舍分类");
    }

    /**
     * 更新宿舍分类
     * @param category
     * @return
     */
    @PutMapping
    public Result UpdateCategory(@RequestBody Category category){
        categoryService.updateCategory(category);
        return Result.success("更新宿舍分类成功");
    }

    /**
     * 获取宿舍分类
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result GetCategory(@RequestParam Integer id){
        Category category = categoryService.GetCategoryById(id);
        return Result.success(category);
    }

    /**
     * 删除宿舍分类
     * @param id
     * @return
     */
    @DeleteMapping
    public Result DeleteCategory(@RequestParam Integer id){
        categoryService.deleteCategoryById(id);
        return Result.success("删除宿舍分类成功");
    }

    /**
     * 导出
     * @param response
     */
    @GetMapping("/exportData")
    @CrossOrigin("http://localhost:5173")
    public void exportData(HttpServletResponse response) {
        categoryService.exportData(response);



    }

    /**
    TODO* 导入
     * @param file
     * @return
     */
    @PostMapping("importData")
    public Result importData(MultipartFile file) {
//        categoryService.importData(file);
        return Result.success("导入成功");
    }

    /**
     * 根据分类id查询分类名
     */
    @GetMapping("findCategoryName")
    public Result findCategoryName(@RequestParam Integer id){
        Category category = new Category();
        category.setCategoryName(categoryService.findCategoryName(id));
        return Result.success(category.getCategoryName());
    }
}
